package com.hpjz.emrecord.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.hpjz.basicofing.bean.UserBean;
import com.hpjz.basicofing.dao.IUserDao;
import com.hpjz.common.SysConfig;
import com.hpjz.common.data.PageData;
import com.hpjz.common.utils.DateUtil;
import com.hpjz.emrecord.bean.EmRecordBean;
import com.hpjz.emrecord.bean.PatientInfoBean;
import com.hpjz.emrecord.bean.QueryEmRecordBean;
import com.hpjz.emrecord.bean.ResourcesBean;
import com.hpjz.emrecord.dao.EmRecordDao;
import com.hpjz.emrecord.dao.PatientInfoDao;
import com.hpjz.emrecord.dao.ResourcesDao;
import com.hpjz.emrecord.service.IEmRecordService;
import com.hpjz.emrecord.web.vo.EmRecordVO;
import com.hpjz.emrecord.web.vo.PatientInfoVO;
import com.hpjz.emrecord.web.vo.ResourcesVO;

@Service("emRecordService")
public class EmRecordServiceImpl implements IEmRecordService {

	@Resource
	private EmRecordDao emRecordDao;
	
	@Resource
	private PatientInfoDao patientInfoDao;
	
	@Resource
	private ResourcesDao resourcesDao;
	
	@Resource
	private IUserDao  userDao;
	
	@Override
	public Map<String,Long> insertEmRecord(PatientInfoVO vo) {
		EmRecordVO evo = vo.getPemrecord();
	
		EmRecordBean bean = this.voToBean(evo);
		//更改电子病历状态
		 EmRecordBean status = new EmRecordBean();
		 if (StringUtils.isNotBlank(bean.getCarlicensplate())) {
			 status.setCarlicensplate(bean.getCarlicensplate());
			 status.setStatus("0");
		 }
		this.emRecordDao.update(status);
		//直接添加用户，返回用户主键，再添加病历。
		PatientInfoBean pbean =new PatientInfoBean();
		BeanUtils.copyProperties(vo, pbean);
		if (StringUtils.isNotBlank(vo.getPatientage())) {
			pbean.setPatientage(Integer.valueOf(vo.getPatientage()));
		}
		this.patientInfoDao.insert(pbean);
		
		//插入的用户信息的数据是。
		bean.setPatientid(pbean.getPatientid());
		bean.setCreatetime(new Date());
		bean.setStatus("1");
		this.emRecordDao.insert(bean);
		
		Map<String,Long> resultMap = new HashMap<>();
		resultMap.put("patientid", pbean.getPatientid());
		resultMap.put("recordid", bean.getRecordid());
		return resultMap;
	}
	
    
	/**
	 * 将vo转化为bean
	 * @param vo
	 * @return
	 */
	private EmRecordBean voToBean(EmRecordVO vo) {
		
		EmRecordBean bean = new EmRecordBean();
		BeanUtils.copyProperties(vo, bean);
		if (StringUtils.isNotBlank(vo.getRecordid())) {
			bean.setRecordid(Long.valueOf(vo.getRecordid()));
		}
		if (StringUtils.isNotBlank(vo.getPatientid())) {
			bean.setPatientid(Long.valueOf(vo.getPatientid()));
		}
		
		if (StringUtils.isNotBlank(vo.getDepaturetime())) {
			bean.setDepaturetime(DateUtil.parse(vo.getDepaturetime()));
		}
		
		if (StringUtils.isNotBlank(vo.getArrifieldtime())) {
			bean.setArrifieldtime(DateUtil.parse(vo.getArrifieldtime()));
		}
		
		if (StringUtils.isNotBlank(vo.getLeavefieldtime())) {
			bean.setLeavefieldtime(DateUtil.parse(vo.getLeavefieldtime()));
		}
		
		if (StringUtils.isNotBlank(vo.getArridesttime())) {
			bean.setArridesttime(DateUtil.parse(vo.getArridesttime()));
		}
		
		if (StringUtils.isNotBlank(vo.getCreatetime())) {
			bean.setCreatetime(DateUtil.parse(vo.getCreatetime()));
		}
		
		if (StringUtils.isNotBlank(vo.getOr_id())) {
			bean.setOr_id(Long.valueOf(vo.getOr_id()));
		}
		
		if (StringUtils.isNotBlank(vo.getCompletetime())) {
			bean.setCompletetime(DateUtil.parse(vo.getCompletetime()));
		}
		
		if (StringUtils.isNotBlank(vo.getLeavedistance())) {
			bean.setLeavedistance(new BigDecimal(vo.getLeavedistance()));
		}
		
		
		return bean;
	}
      
	/**
	 * 根据条件查询电子病历
	 */
	//TODO:添加根据医院条件
	@Override
	public List<EmRecordVO> findEmRecords(EmRecordVO vo) {
		EmRecordBean bean = this.voToBean(vo);
		List<EmRecordBean> ebeans = this.emRecordDao.findList(bean);
		
		List<EmRecordVO> evos = beansToVoList(ebeans);
		
		return evos;
	}


	private List<EmRecordVO> beansToVoList(List<EmRecordBean> ebeans) {
		List<EmRecordVO> evos = new ArrayList<>();
		for (EmRecordBean b : ebeans) {
			EmRecordVO v = this.beanToVo(b);
			evos.add(v);
		}
		return evos;
	}

	@Override
	public Long deleteEmRecord(EmRecordVO vo) {
		EmRecordBean bean = this.voToBean(vo);
		this.emRecordDao.delete(bean);
		return null;
	}

	@Override
	public Long updateEmRecord(PatientInfoVO vo) {
		 //修改个人信息
		 PatientInfoBean pbean = new PatientInfoBean();
		 BeanUtils.copyProperties(vo, pbean);
		 pbean.setPatientid(Long.valueOf(vo.getPatientid()));
		 if (StringUtils.isNotBlank(vo.getPatientage())) {
		  pbean.setPatientage(Integer.valueOf(vo.getPatientage()));	
	     }
		 this.patientInfoDao.update(pbean);
		 //修改电子病历
		 EmRecordBean ebean = this.voToBean(vo.getPemrecord());
		 this.emRecordDao.update(ebean);
		 return 0L;
	}


	@Override
	public EmRecordVO findEmRecord(EmRecordVO vo) {
		EmRecordBean search = this.voToBean(vo);
		EmRecordBean bean = this.emRecordDao.findOne(search);
		ResourcesBean rbean = new ResourcesBean();
		
	
		
		List<ResourcesVO> vos = new ArrayList<>();
		
		if (bean.getRecordid()!=null) {
			rbean.setRecordid(bean.getRecordid());
		//查询用户资源
		List<ResourcesBean> res = this.resourcesDao.findList(rbean);
		for (ResourcesBean r : res) {
			ResourcesVO rvo = new ResourcesVO();
			rvo.setResourceid(String.valueOf(r.getResourceid()));
			rvo.setFilename("/fileupload/"+r.getFilename());
			rvo.setImagetype(r.getImagetype());
			rvo.setRecordid(String.valueOf(r.getRecordid()));
		    vos.add(rvo);
		 }
		}
		
		EmRecordVO evo = new EmRecordVO();
		//根据用户uid查询用户信息
		String uid = bean.getCreatorid();
		UserBean ubean = new UserBean();
		if (StringUtils.isNotBlank(uid)) {
			ubean.setUid(uid);
		}
		
		UserBean userbean = this.userDao.findOne(ubean);
		
		if (bean!=null) {
		    evo = this.beanToVo(bean);
		    evo.setResources(vos);
		    if (userbean!=null) {
				if (StringUtils.isNotBlank(userbean.getUsername())) {
				    evo.setCreatorName(userbean.getUsername());
				}
			}
		}
		return evo;
	}


	private EmRecordVO beanToVo(EmRecordBean bean) {
		EmRecordVO evo = new EmRecordVO();
		if(bean != null){
			BeanUtils.copyProperties(bean, evo);
			if(evo != null && StringUtils.isNotBlank(evo.getRecordid())){
				List<ResourcesVO> list = emRecordDao.findResourcesByRecordid(evo.getRecordid());
			    if(!CollectionUtils.isEmpty(list)){
			    	for(ResourcesVO resourcesVO:list){
			    		if(resourcesVO != null){
			    			resourcesVO.setFilename("/"+resourcesVO.getFilename());
			    		}
			    	}
			    	evo.setResources(list);
			    }
			}
			
		}
		
		evo.setArridesttime(DateUtil.dateToStr(bean.getArridesttime()));
		evo.setArrifieldtime(DateUtil.dateToStr(bean.getArrifieldtime()));
		evo.setLeavefieldtime(DateUtil.dateToStr(bean.getLeavefieldtime()));
		evo.setCompletetime(DateUtil.dateToStr(bean.getCompletetime()));
		evo.setCreatetime(DateUtil.dateToStr(bean.getCreatetime()));
		evo.setDepaturetime(DateUtil.dateToStr(bean.getDepaturetime()));
		evo.setOr_id(String.valueOf(bean.getOr_id()));
		evo.setPatientid(String.valueOf(bean.getPatientid()));
		evo.setRecordid(String.valueOf(bean.getRecordid()));
		//患者相关信息
		PatientInfoBean pbean = bean.getPatientInfo();
		if (pbean != null) {
			BeanUtils.copyProperties(pbean, evo);
			Integer  age = pbean.getPatientage();
			if (age!=null) {
			 evo.setPatientage(String.valueOf(age));
			}
		}
		
		return evo;
	}


	@Override
	public Long updateEmRecordStatus(String recordid) {
		EmRecordBean bean = new EmRecordBean();
		bean.setRecordid(Long.valueOf(recordid));
		bean.setStatus("0");
		long id = this.emRecordDao.update(bean);
		return null;
	}


	@Override
	public Map findEmRecordsPage(Map<String, String> map) {
		EmRecordBean bean = new EmRecordBean();
		String currentPage = map.get("currentPage");
		String beginTime = map.get("begintime");
		String endTime = map.get("endtime");
		String uid = map.get("uid");
		String platform = map.get("platform");
		if (StringUtils.isNotBlank(beginTime)) {
			bean.setBegintime(DateUtil.parse(beginTime));
		}
		if (StringUtils.isNotBlank(endTime)) {
			bean.setEndtime(DateUtil.parse(endTime));
		}
		
		//根据用户查询所在医院
		UserBean b = new UserBean();
		b.setUid(uid);
		b.setPlatform(platform);
		UserBean user = this.userDao.findOne(b);
		/*Long  orgId = user.getOrg_id();
		if (orgId!=null) {
			bean.setOr_id(orgId);
		}*/
		
		Map dataMap = new HashMap<>();
		
		System.out.println("---user--"+user);
		
		if (user!=null) {
			bean.setOr_id(user.getOrg_id());
			PageData data = new PageData();

			data.setCurrentPage(Integer.valueOf(currentPage));
			int offIndex = data.getOffIndex();

			bean.setOffIndex(offIndex);
			bean.setPageSize(data.getPageSize());
			List<EmRecordBean> rebeans = this.emRecordDao.findEmByPage(bean);
			List<EmRecordVO> vos = this.beansToVoList(rebeans);
			data.setData(vos);
			// 查询所有的条数
			Integer totalCount = this.emRecordDao.findEmcount(bean);
			data.setTotalCount(totalCount);
			dataMap.put("data", data);
		}else{
			dataMap.put("error", "error");
		}
	   
		return dataMap;
	}

}
